package
{
	import enemies.*;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import units.Unit;
	
	public class GameHandler extends MovieClip
	{
		public var STAGE_WIDTH = 800, STAGE_HEIGHT = 600;
		public var player_castle:Castle;
		public var mouse_x=0,mouse_y=0;
		public var player_units:Array = new Array();
		public var enemy_units:Array = new Array();
		public var projectiles:Array = new Array();
		public var castle_center:Point;
		public var magic_ui:Magic_UI;
		public var mana_amount = 1000;
		public var resize_effect = 1;
		
		public function GameHandler()
		{
			super();
			setup_level();
			setup_ui();
			stage.addEventListener(Event.ENTER_FRAME, run_frame);
			//stage.addEventListener(Event.ENTER_FRAME, level_timer);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouse_position);
			fpsCounter();
		}
		public function setup_ui(){
			magic_ui = new Magic_UI(this);
			this.addChild(magic_ui);
			magic_ui.x = 10;
			magic_ui.y = STAGE_HEIGHT - magic_ui.height - 10;
		}
		public function setup_level(){
			player_castle = new Castle(this);
			addChild(player_castle);
			player_castle.x = (STAGE_WIDTH - Castle.BASE_WIDTH);
			player_castle.y = (STAGE_HEIGHT - Castle.BASE_HEIGHT);
			castle_center = new Point(player_castle.x,player_castle.y);
			
			for(var i = 0; i < 3; i ++){
				var unit = new Unit(this,player_castle);
				unit.x = 363+25 * i;
				unit.y = 300;
			}
			
		}
		
		public function generate_enemy(type,loc_x,loc_y){
			if(type == 'yellow_slime'){
				var slime = new Yellow_Slime(this);
				slime.x=loc_x;slime.y=loc_y;
				addChild(slime);
			}
			if(type == 'green_slime'){
				var slime2 = new Green_Slime(this);
				slime2.x=loc_x;slime2.y=loc_y;
				addChild(slime2);
			}
			if(type == 'spitting_slime'){
				var slimeS = new Spitting_Slime(this);
				slimeS.x=loc_x;slimeS.y=loc_y;
				addChild(slimeS);
			}
			if(type == 'red_slime'){
				var slime3 = new Red_Slime(this);
				slime3.x=loc_x;slime3.y=loc_y;
				slime3.scaleX = .5; slime3.scaleY = .5;
				addChild(slime3);
			}
		}
		
		public function generate_player_unit(){
			var unit = new Unit(this,player_castle);
			unit.x = 388;
			unit.y = 300;
		}
		
		public var time_passed = 0;
		public function level_timer(evt){
			time_passed ++;
			if(time_passed % 25 == 0){
				var side = Math.floor(Math.random()*4);
				var slime_type = Math.floor(Math.random()*3);
				var slime;
				if(slime_type == 0){
					slime = new Red_Slime(this);
					slime.scaleX = .5;
					slime.scaleY = .5
				}
				if(slime_type == 1)
					slime = new Yellow_Slime(this);
				if(slime_type == 2)
					slime = new Green_Slime(this);
				if(side == 0){
					slime.x = Math.random()*STAGE_WIDTH; slime.y = 0;
				}else if (side == 1){
					slime.x = STAGE_WIDTH; slime.y = Math.random()*STAGE_HEIGHT;
				}
				else if (side == 2){
					slime.x = Math.random()*STAGE_WIDTH; slime.y = STAGE_HEIGHT;
				}else if (side == 3){
					slime.x = 0; slime.y = Math.random()*STAGE_HEIGHT;
				}
				addChild(slime);
				if(slime_type == 0){
					for(var i = 0; i < 5; i++){
						var slime2 = new Red_Slime(this);
						slime2.scaleX = .5;
						slime2.scaleY = .5;
						slime2.x = slime.x + 8 + Math.floor(Math.random()*8);slime2.y = slime.y - Math.floor(Math.random()*6);
						addChild(slime2);
					}
				}
			}
			
		}
		
		public function run_frame(evt){
			for(var i:int = 0; i < player_units.length; i++)
				player_units[i].run();
			for(i = 0; i < enemy_units.length; i++)
				enemy_units[i].run();
			for(i = 0; i < projectiles.length; i++)
				projectiles[i].run();
			magic_ui.run();
		}
		public function mouse_position(event:MouseEvent) {
			mouse_x = mouseX;
			mouse_y = mouseY;
		}
		
		//fps stuff
		var startTime:Number;  
		var framesNumber:Number = 0;  
		public var fps;  
		
		function fpsCounter():void  
		{  
			startTime = getTimer();  
			addChild(fps);  
			
			addEventListener(Event.ENTER_FRAME, checkFPS);  
		}  
		
		function checkFPS(e:Event):void  
		{  
			var currentTime:Number = (getTimer() - startTime) / 1000;  
			
			framesNumber++;  
			
			if (currentTime > 1)  
			{  
				fps.text = "FPS: " + (Math.floor((framesNumber/currentTime)*10.0)/10.0);  
				startTime = getTimer();  
				framesNumber = 0;  
			}  
		}  
	}
}